<?php
class Z1chucvucbsController extends AppController {

	var $name = 'Z1chucvucbs';
    var $uses = array('Z1chucvucb', 'Tgiachidoancb','Chucv','Chucvucb');

	function index($chucv_id=0,$canbo_id=0) {
		$this->Z1chucvucb->recursive = 0;
		$this->set('z1chucvucbs', $this->paginate(array('Chidoansv.khoa_id'=>$this->Session->read('khoa_id'),
                                                            'ngayketthuc'=>NULL)));
        $tenkhoa=$this->Z1chucvucb->query("select tenkhoa from khoas where id=".$this->Session->read('khoa_id'));
        $canbo=$this->Tgiachidoancb->find('list',array('conditions'=>array('ngaychuyen'=>NULL,'Chidoansv.khoa_id'=>$this->Session->read('khoa_id')),
        													'recursive'=>1,
                                                    		'fields'=>array('canbo_id','Canbo.hoten')));
        $chucvu=$this->Chucv->find('list',array('fields'=>array('tenchucvu')));
        $this->set(compact('tenkhoa','canbo','chucvu','chucv_id','canbo_id'));
	}
    
    function luu() {
		if (!empty($this->data)) {
			$this->Chucvucb->create();
                $record['Chucvucb']['chucv_id']=$this->data['Z1chucvucb']['chucvu'];
                $record['Chucvucb']['canbo_id']=$this->data['Z1chucvucb']['canbo'];
                $record['Chucvucb']['ngaybatdau']=date('Y-m-d');
			if ($this->Chucvucb->save($record)) {
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Thông tin không thể lưu', true),'error');
			}
		}
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid z1chucvucb', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('z1chucvucb', $this->Z1chucvucb->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Z1chucvucb->create();
			if ($this->Z1chucvucb->save($this->data)) {
				$this->Session->setFlash(__('The z1chucvucb has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The z1chucvucb could not be saved. Please, try again.', true));
			}
		}
		$canbos = $this->Z1chucvucb->Canbo->find('list');
		$chucvs = $this->Z1chucvucb->Chucv->find('list');
		$chidoansvs = $this->Z1chucvucb->Chidoansv->find('list');
		$this->set(compact('canbos', 'chucvs', 'chidoansvs'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid z1chucvucb', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Z1chucvucb->save($this->data)) {
				$this->Session->setFlash(__('The z1chucvucb has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The z1chucvucb could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Z1chucvucb->read(null, $id);
		}
		$canbos = $this->Z1chucvucb->Canbo->find('list');
		$chucvs = $this->Z1chucvucb->Chucv->find('list');
		$chidoansvs = $this->Z1chucvucb->Chidoansv->find('list');
		$this->set(compact('canbos', 'chucvs', 'chidoansvs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for z1chucvucb', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Z1chucvucb->delete($id)) {
			$this->Session->setFlash(__('Z1chucvucb deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Z1chucvucb was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
